//
//  EditAccountViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/9.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "EditAccountViewController.h"
#import "AccountNameViewController.h"
@interface EditAccountViewController ()
{
    cellData *_selectData;
}
@property(nonatomic,strong)ykTableView *tableView;
@end

@implementation EditAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.navigationItem.title = @"编辑账户";
    
    UIButton *leftButton=(UIButton *)self.navigationItem.leftBarButtonItem.customView;
    leftButton.selected=YES;
    
    self.tableView=[SingLeton myTableViewWithFrame:CGRectMake(0, TOP_HEIGHT+10, SCREEN_WIDTH, SCREEN_HEIGHT-TOP_HEIGHT-10)  withRefresh:NO didSelect:^(ykTableView *tableView, NSIndexPath *indexPath) {
       
        
        UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        NSArray *sectionArr=self.tableView.dataArray[indexPath.section];
        
        _selectData=sectionArr[indexPath.row];
        if ([cell.textLabel.text isEqualToString:@"账户名"]||[cell.textLabel.text isEqualToString:@"账号"]||[cell.textLabel.text isEqualToString:@"联系人"]||[cell.textLabel.text isEqualToString:@"联系电话"]||[cell.textLabel.text isEqualToString:@"地址"]||[cell.textLabel.text isEqualToString:@"门店名称"]||[cell.textLabel.text isEqualToString:@"电动车品牌"]) {
            AccountNameViewController *vc=[[AccountNameViewController alloc]init];
            vc.type=cell.textLabel.text;
            vc.content=cell.detailTextLabel.text;
            [self.navigationController pushViewController:vc animated:YES];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getReturnInfo:) name:@"填写回调" object:nil];
        }
        
        
        
    }];
    [self.view addSubview:self.tableView];
    
    if ([self.type isEqualToString:@"管理员"]) {
        
        
        
        
        self.tableView.className=@"Type1";
        NSMutableArray *itemArr=[NSMutableArray array];
        NSMutableArray *itemArr2=[NSMutableArray array];
        cellData *dataObj=[[cellData alloc]init];
        dataObj.text=@"角色类型";
        dataObj.detailText=self.type;
        
        
        
        [itemArr addObject:dataObj];
        
        
        cellData *dataObj3=[[cellData alloc]init];
        dataObj3.text=@"账号";
        dataObj3.detailText=self.model.loginId;
        
        
        cellData *dataObj4=[[cellData alloc]init];
        dataObj4.text=@"账户名";
        dataObj4.detailText=self.model.userTitle;
        
        [itemArr2 addObject:dataObj3];
        [itemArr2 addObject:dataObj4];
        
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView.dataArray addObject:itemArr2];
    }else if ([self.type isEqualToString:@"门店"])
    {
        
        
//        NSString *URLString = [NSString stringWithFormat:@"/axq/admin/%@",self.storeDic.model.someId];
//
//        [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:nil RequestSuccess:^(NSURLResponse *response, id responseObject) {
//            LRLog(@"%@",responseObject);
//            NSString *code = responseObject[@"code"];
//            if([code isEqualToString:@"10008"]){
//                NSDictionary *data=responseObject[@"data"];
//            }
//            else
//            {
//                NSString *str=responseObject[@"msg"];
//
//                [self.view makeToast:str];
//            }
//        } RequestFailed:^(NSError *error) {
//
//        }];
        
        
        
        
        
        
        self.tableView.className=@"Type1";
        NSMutableArray *itemArr=[NSMutableArray array];
        NSMutableArray *itemArr2=[NSMutableArray array];
        
        
        cellData *dataObj=[[cellData alloc]init];
        dataObj.text=@"角色类型";
        dataObj.detailText=@"经销商门店";
        
        
        
        cellData *dataObj3=[[cellData alloc]init];
        dataObj3.text=@"账号";
        dataObj3.detailText=self.model.loginId;
        
        [itemArr addObject:dataObj];
        
        [itemArr addObject:dataObj3];
        
        
        
        
        cellData *dataObj4=[[cellData alloc]init];
        dataObj4.text=@"门店名称";
        dataObj4.detailText=self.model.userTitle;
        
        cellData *dataObj5=[[cellData alloc]init];
        dataObj5.text=@"联系人";
        dataObj5.detailText=self.model.contacts;
        
        cellData *dataObj6=[[cellData alloc]init];
        dataObj6.text=@"联系电话";
        dataObj6.detailText=self.model.telephone;
        
        
        cellData *dataObj7=[[cellData alloc]init];
        dataObj7.text=@"地址";
        dataObj7.detailText=self.model.address;
        
        [itemArr2 addObject:dataObj4];
        [itemArr2 addObject:dataObj5];
        [itemArr2 addObject:dataObj6];
        [itemArr2 addObject:dataObj7];
        
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView.dataArray addObject:itemArr2];
        
    }
    else if ([self.type isEqualToString:@"修理商"])
    {
        self.tableView.className=@"Type1";
        NSMutableArray *itemArr=[NSMutableArray array];
        NSMutableArray *itemArr2=[NSMutableArray array];
        
        
        cellData *dataObj=[[cellData alloc]init];
        dataObj.text=@"角色类型";
        dataObj.detailText=self.type;
        
        
        
        cellData *dataObj3=[[cellData alloc]init];
        dataObj3.text=@"账号";
        dataObj3.detailText=@"13800000000";
        
        [itemArr addObject:dataObj];
        
        [itemArr addObject:dataObj3];
        
        
        
        
        cellData *dataObj4=[[cellData alloc]init];
        dataObj4.text=@"账户名";
        dataObj4.detailText=self.model.userTitle;
        
        cellData *dataObj5=[[cellData alloc]init];
        dataObj5.text=@"联系人";
        dataObj5.detailText=@"某某某";
        
        cellData *dataObj6=[[cellData alloc]init];
        dataObj6.text=@"联系电话";
        dataObj6.detailText=@"13800000000";
        
        
        cellData *dataObj7=[[cellData alloc]init];
        dataObj7.text=@"地址";
        dataObj7.detailText=self.model.address;
        
        [itemArr2 addObject:dataObj4];
        [itemArr2 addObject:dataObj5];
        [itemArr2 addObject:dataObj6];
        [itemArr2 addObject:dataObj7];
        
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView.dataArray addObject:itemArr2];
        
    }
    else if ([self.type isEqualToString:@"电动车厂商"])
    {
        self.tableView.className=@"Type1";
        NSMutableArray *itemArr=[NSMutableArray array];
        NSMutableArray *itemArr2=[NSMutableArray array];
        NSMutableArray *itemArr3=[NSMutableArray array];
        
        
        cellData *dataObj=[[cellData alloc]init];
        dataObj.text=@"角色类型";
        dataObj.detailText=self.type;
        
       
        
        cellData *dataObj3=[[cellData alloc]init];
        dataObj3.text=@"账号";
        dataObj3.detailText=self.model.loginId;
        
        [itemArr addObject:dataObj];
        
        [itemArr addObject:dataObj3];
        
        
        
        
        cellData *dataObj4=[[cellData alloc]init];
        dataObj4.text=@"电动车品牌";
        dataObj4.detailText=self.model.userTitle;
        
        cellData *dataObj5=[[cellData alloc]init];
        dataObj5.text=@"联系人";
        dataObj5.detailText=self.model.contacts;
        
        cellData *dataObj6=[[cellData alloc]init];
        dataObj6.text=@"联系电话";
        dataObj6.detailText=self.model.telephone;
        
        
        cellData *dataObj7=[[cellData alloc]init];
        dataObj7.text=@"地址";
        dataObj7.detailText=self.model.address;
        
        [itemArr2 addObject:dataObj4];
        [itemArr2 addObject:dataObj5];
        [itemArr2 addObject:dataObj6];
        [itemArr2 addObject:dataObj7];
        
        
        cellData *dataObj8=[[cellData alloc]init];
        dataObj8.text=@"电动车品牌";
        dataObj8.detailText=@"填写品牌名称";
        
        [itemArr3 addObject:dataObj8];
        
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView.dataArray addObject:itemArr2];
//        [self.tableView.dataArray addObject:itemArr3];
        
    }
    else if ([self.type isEqualToString:@"民警"])
    {
        self.tableView.className=@"Type1";
        NSMutableArray *itemArr=[NSMutableArray array];
        NSMutableArray *itemArr2=[NSMutableArray array];
        
        
        cellData *dataObj=[[cellData alloc]init];
        dataObj.text=@"角色类型";
        dataObj.detailText=self.type;
        
        
        
        cellData *dataObj3=[[cellData alloc]init];
        dataObj3.text=@"账号";
        dataObj3.detailText=self.model.loginId;
        
        [itemArr addObject:dataObj];
        
        [itemArr addObject:dataObj3];
        
        
        
        
        cellData *dataObj4=[[cellData alloc]init];
        dataObj4.text=@"账户名";
        dataObj4.detailText=self.model.userTitle;
        
        
        
        cellData *dataObj6=[[cellData alloc]init];
        dataObj6.text=@"联系电话";
        dataObj6.detailText=self.model.telephone;
        
        
        cellData *dataObj7=[[cellData alloc]init];
        dataObj7.text=@"省市区";
        dataObj7.detailText=@"选择地区";
        
        [itemArr2 addObject:dataObj4];
       
        [itemArr2 addObject:dataObj6];
//        [itemArr2 addObject:dataObj7];
        
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView.dataArray addObject:itemArr2];
        
    }
    else if ([self.type isEqualToString:@"物业"])
    {
        self.tableView.className=@"Type1";
        NSMutableArray *itemArr=[NSMutableArray array];
        NSMutableArray *itemArr2=[NSMutableArray array];
        
        
        cellData *dataObj=[[cellData alloc]init];
        dataObj.text=@"角色类型";
        dataObj.detailText=self.type;
        
        
        
        cellData *dataObj3=[[cellData alloc]init];
        dataObj3.text=@"账号";
        dataObj3.detailText=self.model.loginId;
        
        [itemArr addObject:dataObj];
        
        [itemArr addObject:dataObj3];
        
        
        
        
        cellData *dataObj4=[[cellData alloc]init];
        dataObj4.text=@"账户名";
        dataObj4.detailText=self.model.userTitle;
        
        cellData *dataObj5=[[cellData alloc]init];
        dataObj5.text=@"联系人";
        dataObj5.detailText=self.model.contacts;
        
        cellData *dataObj6=[[cellData alloc]init];
        dataObj6.text=@"联系电话";
        dataObj6.detailText=self.model.telephone;
        
        
        
        
        [itemArr2 addObject:dataObj4];
        [itemArr2 addObject:dataObj5];
        [itemArr2 addObject:dataObj6];
        
        
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView.dataArray addObject:itemArr2];
        
    }
    
    [self.tableView reloadData];
    self.tableView.tableFooterView=[self customFootView];
    
    
}

-(UIView *)customFootView
{
    UIView *footView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    
    UIButton *saveBtn=[SingLeton endButton:[UIColor colorQ] andTitle:@"保存" RequestSuccess:^(UIButton *btn) {
        
        NSString *URLString = @"/axq/admin/save";
        /*
         1 uid
         2 loginid  登录手机号
         4 userrole 角色（+)
         5 usertitle 账户名称
         6 contact 联系人
         7 address 地址
         8 telephone 联系电话
         9 headimg 头像url
         
         */
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:self.model.someId forKey:@"uid"];
        for (int i=0; i<self.tableView.dataArray.count; i++) {
            if ([self.tableView.dataArray[i] isKindOfClass:[NSArray class]]) {
                NSArray *sectionArr=self.tableView.dataArray[i];
                for (int i=0; i<sectionArr.count; i++) {
                    cellData *dataObj=sectionArr[i];
                    if ([dataObj.text isEqualToString:@"角色类型"]) {
                        if ([dataObj.detailText isEqualToString:@"管理员"]) {
                            [dic setObject:@"admin" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"门店"])
                        {
                            [dic setObject:@"shop" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"修理商"])
                        {
                            [dic setObject:@"repair" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"电动车厂商"])
                        {
                            [dic setObject:@"producer" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"民警"])
                        {
                            [dic setObject:@"police" forKey:@"userrole"];
                        }
                        else if ([dataObj.detailText isEqualToString:@"物业"])
                        {
                            [dic setObject:@"property" forKey:@"userrole"];
                        }
                    }
                    else if ([dataObj.text isEqualToString:@"账号"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"loginid"];
                    }
                    else if ([dataObj.text isEqualToString:@"账户名"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"usertitle"];
                    }
                    else if ([dataObj.text isEqualToString:@"联系人"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"contact"];
                    }
                    else if ([dataObj.text isEqualToString:@"联系电话"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"telephone"];
                    }
                    else if ([dataObj.text isEqualToString:@"地址"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"address"];
                    }
                    else if ([dataObj.text isEqualToString:@"门店名称"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"usertitle"];
                    }
                    else if ([dataObj.text isEqualToString:@"电动车品牌"])
                    {
                        [dic setObject:dataObj.detailText forKey:@"usertitle"];
                    }
                    
                }
            }else
            {
                cellData *dataObj=self.tableView.dataArray[i];
                if ([dataObj.text isEqualToString:@"角色类型"]) {
                    
                    if ([dataObj.detailText isEqualToString:@"管理员"]) {
                        [dic setObject:@"admin" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"门店"])
                    {
                        [dic setObject:@"shop" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"修理商"])
                    {
                        [dic setObject:@"repair" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"电动车厂商"])
                    {
                        [dic setObject:@"producer" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"民警"])
                    {
                        [dic setObject:@"police" forKey:@"userrole"];
                    }
                    else if ([dataObj.detailText isEqualToString:@"物业"])
                    {
                        [dic setObject:@"property" forKey:@"userrole"];
                    }
                }
                else if ([dataObj.text isEqualToString:@"账号"])
                {
                    [dic setObject:dataObj.detailText forKey:@"loginid"];
                }
                else if ([dataObj.text isEqualToString:@"账户名"])
                {
                    [dic setObject:dataObj.detailText forKey:@"usertitle"];
                }
                else if ([dataObj.text isEqualToString:@"联系人"])
                {
                    [dic setObject:dataObj.detailText forKey:@"contact"];
                }
                else if ([dataObj.text isEqualToString:@"联系电话"])
                {
                    [dic setObject:dataObj.detailText forKey:@"telephone"];
                }
                else if ([dataObj.text isEqualToString:@"地址"])
                {
                    [dic setObject:dataObj.detailText forKey:@"address"];
                }
                else if ([dataObj.text isEqualToString:@"门店名称"])
                {
                    [dic setObject:dataObj.detailText forKey:@"usertitle"];
                }
                else if ([dataObj.text isEqualToString:@"电动车品牌"])
                {
                    [dic setObject:dataObj.detailText forKey:@"usertitle"];
                }
            }
        }
        
        [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:dic RequestSuccess:^(NSURLResponse *response, id responseObject) {
            NSLog(@"%@",responseObject);
            NSString *code = responseObject[@"code"];
            if([code isEqualToString:@"10008"]){
                //                NSDictionary *data=responseObject[@"data"];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                NSString *str=responseObject[@"msg"];
                
                [self.view makeToast:str];
            }
        } RequestFailed:^(NSError *error) {
            
        }];
        
        
        
        
    }];
    [footView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-60)/2, 44));
    }];
    
    UIButton *deleteBtn=[SingLeton endButton:[UIColor colorWithHexString:@"ff556d"] andTitle:@"删除" RequestSuccess:^(UIButton *btn) {
        NSString *URLString = [NSString stringWithFormat:@"/axq/admin/delete/%@",self.model.someId];
        
        [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:nil RequestSuccess:^(NSURLResponse *response, id responseObject) {
            LRLog(@"%@",responseObject);
            NSString *code = responseObject[@"code"];
            if([code isEqualToString:@"10008"]){
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                NSString *str=responseObject[@"msg"];
                
                [self.view makeToast:str];
            }
        } RequestFailed:^(NSError *error) {
            
        }];
    }];
    [footView addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(saveBtn);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH-60)/2, 44));
    }];
    return footView;
}
-(void)getReturnInfo:(NSNotification *)notic
{
    
    
    _selectData.detailText=notic.userInfo[@"backinfo"];
    [self.tableView reloadData];
    
    
    
    
}
@end
